package com.education.teach.mvp.presenter;

import android.app.Application;
import android.widget.Toast;

import com.education.teach.mvp.model.bean.CounselBean;
import com.education.teach.mvp.model.bean.MyClassBean;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.education.teach.mvp.contract.MyWorksContract;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.HashMap;
import java.util.Map;


@ActivityScope
public class MyWorksPresenter extends BasePresenter<MyWorksContract.Model, MyWorksContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MyWorksPresenter(MyWorksContract.Model model, MyWorksContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void getMyClassList(String xlh, int page, int maxRow, boolean isRefreh) {
        Map<String, String> map = new HashMap<>();
        map.put("xlh", xlh);
        map.put("currentPage", page + "");
        map.put("maxRows", maxRow + "");
        mModel.getMyClassList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(disposable -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .subscribe(new ErrorHandleSubscriber<MyClassBean>(mErrorHandler) {
                    @Override
                    public void onNext(MyClassBean counselBean) {
                        if (isRefreh)
                            mRootView.returnRefreshClassList(counselBean.getResult().getQueryList());
                        else
                            mRootView.returnLoadMoreClassList(counselBean.getResult().getQueryList());
                    }

                });
    }

    public void share(String id, String state) {
        Map<String, String> map = new HashMap<>();
        map.put("workId", id);
        map.put("state", state);
        mModel.share(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(disposable -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .subscribe(new ErrorHandleSubscriber<MyClassBean>(mErrorHandler) {
                    @Override
                    public void onNext(MyClassBean counselBean) {
                        mRootView.showShareResult(counselBean, state);
                        if ("0".equals(state)){
                            Toast.makeText(mApplication, "取消分享", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(mApplication, "分享成功", Toast.LENGTH_SHORT).show();
                        }
                    }

                });
    }
}
